#include "List.cpp"


ListNode* deleteDuplicates(ListNode* head){
    ListNode* newHead = (ListNode*) malloc(sizeof(ListNode));
    newHead->next = NULL;
    ListNode* pos= newHead;

    ListNode *p = head;
    int pre =  p->data-1;
    while (p != NULL) {
        if (p->data == pre) {
            p = p->next;
            continue;
        }
        // 申请空间并赋值
        ListNode* node = (ListNode*) malloc(sizeof(ListNode));
        node->next = NULL;
        node->data = p->data;
        // 尾插
        pos->next = node;
        pos = pos->next;
        // 更新最后一次插入的数值
        pre = p->data;
        // 下一次循环
        p = p->next;
    }
    return newHead->next;
}

ListNode* deleteDuplicate(ListNode* head){
    ListNode* pre= head;
    ListNode* p = head->next;

    while (p != NULL) {
        if (p->data == pre->data) {
            pre->next = p->next;
            free(p);
            p = pre->next;
        }else {
            pre = pre->next;
            p = p->next;
        }
    }

    return head;
}

int main() {
    int number[] = {1,2,3,3,4,4};
    List list = CreateListByArray(number, ARRAY_SIZE(number));
    List newList = deleteDuplicate(list);
    PrintList(newList);
}
